package com.kalvan.core.exception;

import com.kalvan.core.res.Result;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author chenliang
 */
@Getter
@Setter
@Slf4j
public class BizException extends RuntimeException {
    /**
     * 错误码
     */
    private String code;
    /**
     * 错误描述
     */
    private String msg;

    public BizException(String msg) {
        super(msg);
        this.msg = msg;
        log.error(getMessage());
    }

    public BizException(String code, String msg) {
        super(String.format("code:%s,msg:%s", code, msg));
        this.code = code;
        this.msg = msg;
        log.error(getMessage());
    }

    public BizException(Result result) {
        super(String.format("code:%s,msg:%s", result.getCode(), result.getMsg()));
        this.code = result.getCode();
        this.msg = result.getMsg();
        log.error(getMessage());
    }

    public BizException(ReturnCode returnCode) {
        super(String.format("code:%s,msg:%s", returnCode.getCode(), returnCode.getMsg()));
        this.code = returnCode.getCode();
        this.msg = returnCode.getMsg();
        log.error(getMessage());
    }

    public BizException(ReturnCode returnCode, Throwable cause) {
        super(String.format("code:%s,msg:%s", returnCode.getCode(), returnCode.getMsg()), cause);
        this.code = returnCode.getCode();
        this.msg = returnCode.getMsg();
        log.error(getMessage(), cause);
    }
}
